#include "common.h"

int findMaxLength(vector<int>&nums)
{
	map<int, int> hash({ {0,0} });
	int n = nums.size();
	int sum1 = 0;
	int sum0 = 0;
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		if (nums[i] == 1) sum1++;
		else sum0++;
		if (hash.count(sum1 - sum0))
		{
			ans = max(ans, i + 1 - hash[sum1 - sum0]);
		}
		if (!hash.count(sum1 - sum0))
		{
			hash[sum1 - sum0] = i + 1;
		}
	}
	return ans;
}
